Skip to content

Code Modernization: Use array_key_first() to read the first key of an array - #12790

Open
Soean wants to merge 3 commits into
WordPress:trunkfrom
Soean:code-modernization/array-key-first
Open

Code Modernization: Use array_key_first() to read the first key of an array#12790
Soean wants to merge 3 commits into
WordPress:trunkfrom
Soean:code-modernization/array-key-first

Conversation

@Soean

@Soean Soean commented Jul 31, 2026

Copy link
Copy Markdown
Member

Reading the first key of an array through array_keys() allocates a full array of every key just to keep one entry and discard the rest. array_key_first() reads the first bucket directly.

Follow-up to 65773, which is scoped to the two nested current( array_keys( $array ) ) call sites and is addressed in #12785. This PR covers a second shape of the same idea, which that ticket does not include: the key array is assigned to a variable first, then read on the next line.

$keys    = array_keys( $wp_registered_sidebars );
$sidebar = reset( $keys );
$sidebar = array_key_first( $wp_registered_sidebars );

13 occurrences across 11 files, in two shapes — reset( $keys ) and $keys[0]. In every case the intermediate variable existed only to carry the key array to the next line and is never read again afterwards.

One change that goes further

In spawn_cron() and _wp_cron() the surrounding check is dropped too:

$keys = array_keys( $crons );
if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
if ( array_key_first( $crons ) > $gmt_time ) {

Both functions return early a few lines above when $crons is empty, so isset( $keys[0] ) can never be false there. Happy to keep an explicit null !== $first guard instead if reviewers prefer the defensive form.

Behavior notes

  • On an empty array reset() returns false while array_key_first() returns null. None of the touched call sites compares the result with ===, and most sit behind an empty() guard.
  • Where the old code used $keys[0], the new code is strictly safer: $keys[0] emitted a notice on an empty array, array_key_first() returns null quietly.

… array.

Reading the first key of an array through `array_keys()` builds a complete array of every key only to keep one entry and throw the rest away. Replace that with `array_key_first()`, which reads the first bucket directly.
@github-actions

github-actions Bot commented Jul 31, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props soean, mukesh27.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Comment thread src/wp-admin/includes/media.php Outdated
Comment on lines +1646 to +1645
$keys = array_keys( wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ) );
$type = reset( $keys );
$type = array_key_first( wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ) );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wp_match_mime_types() returns an empty array when nothing matches, an attachment whose mime type falls outside the buckets get_post_mime_types() defines. Previously reset() gave false, and esc_attr( false ) is quiet. Now it's null, and esc_attr()wp_check_invalid_utf8()trim( null ), which is a deprecation notice on PHP 8.1+.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch on the empty array — pushed a guard in b645ef7.

One correction on the mechanism: wp_check_invalid_utf8() starts with (string) $text, so no trim() is involved and the escaping itself stays quiet. The actual leak is the last line of esc_attr(), which passes $text to the attribute_escape filter uncast — a plugin callback there would now get null where it used to get false.

$matched_types = wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type );
$type          = array_key_first( $matched_types ) ?? '';

I went through the other call sites too; this was the only one where the array can actually be empty and the result is used afterwards.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sharing details.

wp_match_mime_types() returns an empty array for an attachment whose mime type falls outside the buckets get_post_mime_types() defines. array_key_first() then yields null where reset() previously yielded false.

The escaping chain itself is unaffected, as both wp_check_invalid_utf8() and _wp_specialchars() cast to string before anything else. But esc_attr() passes the raw value on to the 'attribute_escape' filter as its second argument, documented as string. Core hooks nothing there, while a plugin callback handing that argument to an internal string function would hit the PHP 8.1+ null deprecation that false did not trigger. Default to an empty string, and split the expression so the guard stays visible instead of trailing a 120-character line.
@Soean
Soean force-pushed the code-modernization/array-key-first branch from c321a9a to b645ef7 Compare August 1, 2026 07:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants